package com.huawei.esdk.tp.northcommu.cxf.common;

import java.util.List;
import org.apache.log4j.Logger;
import com.huawei.esdk.platform.common.SDKErrorCode;
import com.huawei.esdk.platform.common.SDKResult;
import com.huawei.esdk.platform.common.exception.SDKException;
import com.huawei.esdk.tp.business.common.confmgr.ConfMgrService;
import com.huawei.esdk.tp.common.ErrInfo;
import com.huawei.esdk.tp.domain.model.Conference;
import com.huawei.esdk.tp.domain.model.ScheduleConference;
import com.huawei.esdk.tp.domain.model.SiteInConference;
import com.huawei.esdk.tp.northcommu.cxf.common.convert.TPCommonConfMgrConvert;
import com.huawei.esdk.tp.northcommu.cxf.common.gen.AddSiteToScheduledConf;
import com.huawei.esdk.tp.northcommu.cxf.common.gen.AddSiteToScheduledConfResponse;
import com.huawei.esdk.tp.northcommu.cxf.common.gen.CancelScheduledConf;
import com.huawei.esdk.tp.northcommu.cxf.common.gen.CancelScheduledConfResponse;
import com.huawei.esdk.tp.northcommu.cxf.common.gen.DelSiteFromScheduledConf;
import com.huawei.esdk.tp.northcommu.cxf.common.gen.DelSiteFromScheduledConfResponse;
import com.huawei.esdk.tp.northcommu.cxf.common.gen.EditScheduledConf;
import com.huawei.esdk.tp.northcommu.cxf.common.gen.EditScheduledConfResponse;
import com.huawei.esdk.tp.northcommu.cxf.common.gen.ProlongScheduledConf;
import com.huawei.esdk.tp.northcommu.cxf.common.gen.ProlongScheduledConfResponse;
import com.huawei.esdk.tp.northcommu.cxf.common.gen.QueryConfSitesStatus;
import com.huawei.esdk.tp.northcommu.cxf.common.gen.QueryConfSitesStatusResponse;
import com.huawei.esdk.tp.northcommu.cxf.common.gen.QueryConferenceStatus;
import com.huawei.esdk.tp.northcommu.cxf.common.gen.QueryConferenceStatusResponse;
import com.huawei.esdk.tp.northcommu.cxf.common.gen.ScheduleConf;
import com.huawei.esdk.tp.northcommu.cxf.common.gen.ScheduleConfResponse;
import com.huawei.esdk.tp.northcommu.cxf.common.gen.ScheduleDynamicSiteConf;
import com.huawei.esdk.tp.northcommu.cxf.common.gen.ScheduleDynamicSiteConfResponse;
import com.huawei.esdk.tp.northcommu.cxf.common.gen.ScheduleImmediateConf;
import com.huawei.esdk.tp.northcommu.cxf.common.gen.ScheduleImmediateConfResponse;
import com.huawei.esdk.tp.northcommu.cxf.common.gen.TPCommonConfMgr;

public class TPCommonConfMgrImpl implements TPCommonConfMgr
{
    private static final Logger LOGGER = Logger
            .getLogger(TPCommonConfMgrImpl.class);

    private TPCommonConfMgrConvert tpCommonConfMgrConvertService;

    private ConfMgrService confMgrService;

    @Override
    public EditScheduledConfResponse editScheduledConf(
            EditScheduledConf parameters)
    {
        SDKResult<ScheduleConference> result = new SDKResult<ScheduleConference>();
        EditScheduledConfResponse editScheduleConfResponse = new EditScheduledConfResponse();
        try
        {
            if(null == parameters || null == parameters.getEditConf() || null == parameters.getEditConf().getSites())
            {
                editScheduleConfResponse.setResultCode(ErrInfo.SDK_PARAM_NOT_COMPLETE_ERRORCODE);
                return editScheduleConfResponse;
            }
            result = confMgrService
                    .editScheduleConfResponseService(tpCommonConfMgrConvertService
                            .editScheduledConfSoap2model(parameters));
            // 将领域bean的返回值改成北向soapbean的返回值

            editScheduleConfResponse = tpCommonConfMgrConvertService
                    .editScheduledConfModel2soap(result);
        }
        catch (SDKException ex)
        {
            editScheduleConfResponse.setResultCode((int) ex.getSdkErrCode());
        }
        catch (Exception e)
        {
            LOGGER.error("editScheduledConf method error", e);
            result.setErrCode(ErrInfo.SDK_SYSTEM_ERRORCODE);
            editScheduleConfResponse.setResultCode((int) result.getErrCode());
        }
        return editScheduleConfResponse;
    }

    @Override
    public ProlongScheduledConfResponse prolongScheduledConf(
            ProlongScheduledConf parameters)
    {
        SDKErrorCode result = new SDKErrorCode();
        ProlongScheduledConfResponse prolongScheduledConfResponse = new ProlongScheduledConfResponse();
        try
        {
            if(null == parameters)
            {
                prolongScheduledConfResponse.setResultCode(ErrInfo.SDK_PARAM_NOT_COMPLETE_ERRORCODE);
                return prolongScheduledConfResponse;
            }
            // 通过领域模型将行业相关bean转到产品无关bean
            result = confMgrService.prolongScheduleConf(parameters.getConfId(),
                    parameters.getProlongTime());

            prolongScheduledConfResponse.setResultCode((int) result
                    .getErrCode());
        }
        catch (SDKException ex)
        {
            prolongScheduledConfResponse
                    .setResultCode((int) ex.getSdkErrCode());
        }
        catch (Exception e)
        {
            LOGGER.error("scheduleDynamicSiteConf method error", e);
            result.setErrCode(ErrInfo.SDK_SYSTEM_ERRORCODE);
            prolongScheduledConfResponse.setResultCode((int) result
                    .getErrCode());
        }
        return prolongScheduledConfResponse;
    }

    @Override
    public QueryConfSitesStatusResponse queryConfSitesStatus(
            QueryConfSitesStatus parameters)
    {
        // 协议相关bean传入service
        SDKResult<List<SiteInConference>> result = new SDKResult<List<SiteInConference>>();
        QueryConfSitesStatusResponse queryConfSitesStatusResponse = null;
        try
        {
            if(null == parameters || null == parameters.getSiteUris())
            {
                queryConfSitesStatusResponse = new QueryConfSitesStatusResponse();
                queryConfSitesStatusResponse.setResultCode(ErrInfo.SDK_PARAM_NOT_COMPLETE_ERRORCODE);
                return queryConfSitesStatusResponse;
            }
            result = confMgrService.queryConfSitesStatus(
                    parameters.getConfId(), parameters.getSiteUris());

            queryConfSitesStatusResponse = tpCommonConfMgrConvertService
                    .queryConfSiteStatusModel2soap(result);
        }
        catch (SDKException ex)
        {
            queryConfSitesStatusResponse = new QueryConfSitesStatusResponse();
            queryConfSitesStatusResponse
                    .setResultCode((int) ex.getSdkErrCode());
        }
        catch (Exception e)
        {
            queryConfSitesStatusResponse = new QueryConfSitesStatusResponse();
            result.setErrCode(ErrInfo.SDK_SYSTEM_ERRORCODE);
            queryConfSitesStatusResponse.setResultCode((int) result
                    .getErrCode());
        }

        return queryConfSitesStatusResponse;
    }

    @Override
    public AddSiteToScheduledConfResponse addSiteToScheduledConf(
            AddSiteToScheduledConf commonRequest)
    {
        AddSiteToScheduledConfResponse commonResponse = null;
        SDKResult<String> result = null;

        try
        {
            if(null == commonRequest)
            {
                commonResponse = new AddSiteToScheduledConfResponse();
                commonResponse.setResultCode(ErrInfo.SDK_PARAM_NOT_COMPLETE_ERRORCODE);
                return commonResponse;
            }
            String confId = commonRequest.getConfId();
            String siteUri = commonRequest.getSiteUri();

            result = confMgrService.addSiteToScheduledConf(confId, siteUri);
        }
        catch (SDKException ex)
        {
            LOGGER.error("addSiteToScheduledConf method error", ex);
            result = new SDKResult<String>();
            result.setErrCode((int) ex.getSdkErrCode());
        }
        catch (Exception e)
        {
            LOGGER.error("addSiteToScheduledConf method error", e);
            result = new SDKResult<String>();
            result.setErrCode(ErrInfo.SDK_SYSTEM_ERRORCODE);
        }

        commonResponse = tpCommonConfMgrConvertService
                .addSiteToScheduledConfModel2NorthSoap(result);

        return commonResponse;
    }

    /**
     * 取消会议（包括正在召开的和未开始的） * @see
     * com.huawei.esdk.tp.northcommu.cxf.common.gen.TPMedicalConfMgr#
     * cancelScheduledConf
     * (com.huawei.esdk.tp.northcommu.cxf.common.gen.CancelScheduledConf)
     */
    @Override
    public CancelScheduledConfResponse cancelScheduledConf(
            CancelScheduledConf parameters)
    {
        CancelScheduledConfResponse response = new CancelScheduledConfResponse();
        try
        {
            if(null == parameters)
            {
                response.setResultCode(ErrInfo.SDK_PARAM_NOT_COMPLETE_ERRORCODE);
                return response;
            }
            SDKErrorCode code = confMgrService.cancelScheduledConf(parameters
                    .getConfId());
            response.setResultCode(Integer.parseInt(String.valueOf(code
                    .getErrCode())));
        }
        catch (SDKException ex)
        {
            LOGGER.error("cancelScheduledConf method error", ex);
            response.setResultCode((int) ex.getSdkErrCode());
        }
        catch (Exception e)
        {
            LOGGER.error("cancelScheduledConf method error", e);
            response.setResultCode(ErrInfo.SDK_SYSTEM_ERRORCODE);
        }
        return response;
    }

    @Override
    public ScheduleConfResponse scheduleConf(ScheduleConf parameters)
    {
        SDKResult<ScheduleConference> result = new SDKResult<ScheduleConference>();
        ScheduleConfResponse scheduleConfResponse = new ScheduleConfResponse();
        try
        {
            if(null == parameters || null == parameters.getScheduleConf() || null == parameters.getScheduleConf().getSites())
            {
                scheduleConfResponse.setResultCode(ErrInfo.SDK_PARAM_NOT_COMPLETE_ERRORCODE);
                return scheduleConfResponse;
            }
            result = confMgrService
                    .scheduleConfResponseService(tpCommonConfMgrConvertService
                            .scheduleConfSoap2model(parameters));
            // 将领域bean的返回值改成北向soapbean的返回值
            scheduleConfResponse = tpCommonConfMgrConvertService
                    .scheduleConferenceModel2soap(result);
        }
        catch (SDKException ex)
        {
            LOGGER.error("ScheduleConference method error", ex);
            scheduleConfResponse.setResultCode((int) ex.getSdkErrCode());
        }
        catch (Exception e)
        {
            LOGGER.error("ScheduleConference method error", e);
            result.setErrCode(ErrInfo.SDK_SYSTEM_ERRORCODE);
            scheduleConfResponse.setResultCode((int) result.getErrCode());
        }
        return scheduleConfResponse;
    }

    @Override
    public DelSiteFromScheduledConfResponse delSiteFromScheduledConf(
            DelSiteFromScheduledConf commonRequest)
    {
        DelSiteFromScheduledConfResponse commonResponse = null;
        SDKErrorCode result = null;
        try
        {
            if(null == commonRequest)
            {
                commonResponse = new DelSiteFromScheduledConfResponse();
                commonResponse.setResultCode(ErrInfo.SDK_PARAM_NOT_COMPLETE_ERRORCODE);
                return commonResponse;
            }
            String confId = commonRequest.getConfId();
            String siteUri = commonRequest.getSiteUri();

            result = confMgrService.delSiteFromScheduledConf(confId, siteUri);
        }
        catch (SDKException ex)
        {
            LOGGER.error("delSiteFromScheduledConf method error", ex);
            result = new SDKErrorCode();
            result.setErrCode((int) ex.getSdkErrCode());
        }
        catch (Exception e)
        {
            LOGGER.error("delSiteFromScheduledConf method error", e);
            result = new SDKErrorCode();
            result.setErrCode(ErrInfo.SDK_SYSTEM_ERRORCODE);
        }

        commonResponse = new DelSiteFromScheduledConfResponse();
        commonResponse.setResultCode((int) result.getErrCode());

        return commonResponse;
    }

    @Override
    public ScheduleDynamicSiteConfResponse scheduleDynamicSiteConf(
            ScheduleDynamicSiteConf parameters)
    {
        SDKResult<ScheduleConference> result = new SDKResult<ScheduleConference>();
        ScheduleDynamicSiteConfResponse scheduleConfResponse = new ScheduleDynamicSiteConfResponse();
        try
        {
            if(null == parameters || null == parameters.getScheduleConf() || null == parameters.getScheduleConf().getSites())
            {
                scheduleConfResponse.setResultCode(ErrInfo.SDK_PARAM_NOT_COMPLETE_ERRORCODE);
                return scheduleConfResponse;
            }
            result = confMgrService
                    .scheduleDynamicSiteConfService(tpCommonConfMgrConvertService
                            .scheduleDynamicConfSoap2model(parameters));
            // 将领域bean的返回值改成北向soapbean的返回值
            scheduleConfResponse = tpCommonConfMgrConvertService
                    .scheduleDynamicConferenceModel2soap(result);
        }
        catch (SDKException ex)
        {
            LOGGER.error("scheduleDynamicSiteConf method error", ex);
            scheduleConfResponse.setResultCode((int) ex.getSdkErrCode());
        }
        catch (Exception e)
        {
            LOGGER.error("scheduleDynamicSiteConf method error", e);
            result.setErrCode(ErrInfo.SDK_SYSTEM_ERRORCODE);
            scheduleConfResponse.setResultCode((int) result.getErrCode());
        }
        return scheduleConfResponse;
    }

    @Override
    public ScheduleImmediateConfResponse scheduleImmediateConf(
            ScheduleImmediateConf parameters)
    {
        SDKResult<ScheduleConference> result = new SDKResult<ScheduleConference>();
        ScheduleImmediateConfResponse scheduleConfResponse = new ScheduleImmediateConfResponse();
        try
        {
            if(null == parameters || null == parameters.getScheduleConf() || null == parameters.getScheduleConf().getSites())
            {
                scheduleConfResponse.setResultCode(ErrInfo.SDK_PARAM_NOT_COMPLETE_ERRORCODE);
                return scheduleConfResponse;
            }
            result = confMgrService
                    .scheduleImmediateConfResponseService(tpCommonConfMgrConvertService
                            .scheduleImmediateConfSoap2model(parameters));
            // 将领域bean的返回值改成北向soapbean的返回值
            scheduleConfResponse = tpCommonConfMgrConvertService
                    .scheduleImmediateConferenceModel2soap(result);
        }
        catch (SDKException ex)
        {
            LOGGER.error("ScheduleConference method error", ex);
            scheduleConfResponse.setResultCode((int) ex.getSdkErrCode());
        }
        catch (Exception e)
        {
            LOGGER.error("ScheduleConference method error", e);
            result.setErrCode(ErrInfo.SDK_SYSTEM_ERRORCODE);
            scheduleConfResponse.setResultCode((int) result.getErrCode());
        }
        return scheduleConfResponse;
    }

    @Override
    public QueryConferenceStatusResponse queryConferenceStatus(
            QueryConferenceStatus parameters)
    {
        QueryConferenceStatusResponse response = new QueryConferenceStatusResponse();
        try
        {
            if(null == parameters)
            {
                response.setResultCode(ErrInfo.SDK_PARAM_NOT_COMPLETE_ERRORCODE);
                return response;
            }
            SDKResult<Conference> result = confMgrService
                    .queryConferencesStatus(parameters.getConfId());
            response = tpCommonConfMgrConvertService
                    .queryConferencesStatusModel2soap(result);
        }
        catch (SDKException ex)
        {
            LOGGER.error("queryConferenceStatus method error", ex);
            response.setResultCode((int)ex.getSdkErrCode());
        }
        catch (Exception e)
        {
            LOGGER.error("", e);
            response.setResultCode(ErrInfo.SDK_SYSTEM_ERRORCODE);
        }
        return response;
    }

    public ConfMgrService getConfMgrService()
    {
        return confMgrService;
    }

    public void setConfMgrService(ConfMgrService confMgrService)
    {
        this.confMgrService = confMgrService;
    }

    public TPCommonConfMgrConvert getTpCommonConfMgrConvertService()
    {
        return tpCommonConfMgrConvertService;
    }

    public void setTpCommonConfMgrConvertService(
            TPCommonConfMgrConvert tpCommonConfMgrConvertService)
    {
        this.tpCommonConfMgrConvertService = tpCommonConfMgrConvertService;
    }
}
